package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.LogInfoService;
import com.the4man.cnbdw.service.RegionInfoService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.cnbdw.utils.SelectModel;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.DataGrid;
import com.the4man.framework.model.LogInfo;
import com.the4man.framework.model.RegionInfo;
import com.the4man.framework.model.RegionInfoExample;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Action(
        value = "regionInfoAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/regionInfo/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/regionInfo/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/regionInfo/edit.jsp"
        )}
)
public class RegionInfoAction extends BaseAction implements ModelDriven<RegionInfo> {
    private RegionInfo regionInfo = new RegionInfo();
    private LogInfo logInfo = new LogInfo();
    @Autowired
    private LogInfoService logService;
    @Autowired
    private RegionInfoService regionInfoService;

    public RegionInfoAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        return "addPage";
    }

    public String toEditPage() {
        try {
            RegionInfo d = (RegionInfo)this.regionInfoService.selectByPrimaryKey(this.getRequest().getParameter("id"), RegionInfo.class);
            BeanUtils.copyProperties(d, this.regionInfo);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            RegionInfoExample regionInfoExample = new RegionInfoExample();
            if(StringUtils.isNotEmpty(this.regionInfo.getName())) {
                regionInfoExample.createCriteria().andNameLike("%" + this.regionInfo.getName() + "%");
            }

            int total = this.regionInfoService.countByExample(regionInfoExample);
            Page<RegionInfo> page = new Page();
            this.setPageProperties(page);
            this.regionInfoService.selectByExample(regionInfoExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal(Long.valueOf((long)total));
            this.writeJson(d);
        } catch (Exception var5) {
            LOG.error("获取数据失败", var5, new String[0]);
        }

    }

    public void add() {
        PageJson json = new PageJson();

        try {
            String name = this.getRequest().getParameter("name");
            RegionInfo region = this.regionInfoService.selectByName(name);
            if(region == null) {
                String time = DateUtil.getInstance().formatDate(new Date());
                this.regionInfo.setId(UUID.randomUUID().toString());
                this.regionInfo.setCreateTime(time);
                this.regionInfo.setUpdateTime(time);
                this.regionInfo.setCreateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.regionInfo.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.regionInfoService.insert(this.regionInfo);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(time);
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("区域管理");
                this.logInfo.setOp("区域添加");
                this.logInfo.setMemo("区域:" + name);
                this.logService.insert(this.logInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("区域名已存在");
            }
        } catch (Exception var5) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var5, new String[0]);
        }

        this.writeJson(json);
    }

    public void update() {
        PageJson json = new PageJson();

        try {
            String name = this.regionInfo.getName();
            String id = this.regionInfo.getId();
            RegionInfo region = this.regionInfoService.selectByMName(id, name);
            if(region == null) {
                String time = DateUtil.getInstance().formatDate(new Date());
                this.regionInfo.setUpdateTime(time);
                this.regionInfo.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.regionInfoService.updateByPrimaryKey(this.regionInfo);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(time);
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("区域管理");
                this.logInfo.setOp("区域修改");
                this.logInfo.setMemo("区域:" + name);
                this.logService.insert(this.logInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("区域名已存在");
            }
        } catch (Exception var6) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var6, new String[0]);
        }

        this.writeJson(json);
    }

    public String executeModifyResult() {
        try {
            Object instance = this.regionInfoService.selectByPrimaryKey(this.regionInfo.getId(), RegionInfo.class);
            this.getRequest().setAttribute("instance", instance);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

        return "modifyPage";
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int count = 0;
            String ids = this.getRequest().getParameter("ids");
            if(ids.contains(",")) {
                String[] str = ids.split(",");
                String[] var8 = str;
                int var7 = str.length;

                for(int var6 = 0; var6 < var7; ++var6) {
                    String info = var8[var6];
                    int i = this.regionInfoService.deleteByPrimaryKey(info, RegionInfo.class);
                    RegionInfo d = (RegionInfo)this.regionInfoService.selectByPrimaryKey(info, RegionInfo.class);
                    if(i > 0) {
                        this.logInfo.setId(UUID.randomUUID().toString());
                        this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                        this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                        this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                        this.logInfo.setFunc("区域管理");
                        this.logInfo.setOp("区域批量删除");
                        this.logInfo.setMemo("区域:" + d.getName());
                        this.logService.insert(this.logInfo);
                        ++count;
                    }
                }
            } else {
                RegionInfo d = (RegionInfo)this.regionInfoService.selectByPrimaryKey(this.getRequest().getParameter("ids"), RegionInfo.class);
                count = this.regionInfoService.deleteByPrimaryKey(this.getRequest().getParameter("ids"), RegionInfo.class);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("区域管理");
                this.logInfo.setOp("区域删除");
                this.logInfo.setMemo("区域:" + d.getName());
                this.logService.insert(this.logInfo);
            }

            if(count > 0) {
                json.setReturnMsg("删除" + count + "个区域成功！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除区域失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var11) {
            LOG.error("批量删除区域失败！", var11, new String[0]);
            json.setReturnMsg("删除区域失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void verify() {
        PageJson json = new PageJson();
        this.writeJson(json);
    }

    public void getRegionInfoSelectData() {
        try {
            List<SelectModel> list = this.regionInfoService.getRegionInfoSelectData();
            this.writeJson(list);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public RegionInfo getModel() {
        return this.regionInfo;
    }
}
